<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Session;
use DB;
use Crypt;
class loginController extends Controller
{
    public function login(Request $request)
    {
    	if ($request -> isMethod('post'))
    	{
    		$code = $request ->input('Acode');
    		if ($code !=Session::get('code')) 
    		{
    			return back() -> with(['info'=>'验证码错误']);
    		}
    		$uname = $request -> input('uname');
    		$res = DB::table('users')->where('uname',$uname)->first();
            $time = Date('Y-m-d');
            $t = ['time'=>$time];
    		// dd($res);
            if (!$res) 
            {
                return back()->with(['info'=>'用户名未注册']);
            } 
    		if ($res->auth == 0) 
    		{
    			return back()->with(['info'=>'您没有权限']);
    		}
    		
    		$upwd = Crypt::decryptString($res->upwd);
    		if ($request->input('upwd') != $upwd) 
    		{
    			return back()->with(['info'=>'用户名或密码错误']);
    		}
    		//判断是否记住我
    		if ($request->has('checkMe')) 
    		{
    			\Cookie::queue('isLogin',$res->upwd,60);
    		}
    	    Session::put('master',$res);
            Session::put('time',$t);
    		return redirect('/admin/user/index')->with(['info'=>'登录成功']);
    	}else
    	{
    		return view('admin.index.login',['title'=>'登录']);
    	}
    	
    }
    public function logout()
    {
    	Session::forget('master');
    	return redirect('/admin/login')->with(['info'=>'登出成功']);
    }
}
